package com.fanyouzhao.eshop.portal.controller;

import com.fanyouzhao.eshop.core.pojo.Goods;
import com.fanyouzhao.eshop.core.pojo.User;
import com.fanyouzhao.eshop.core.utils.JsonResult;
import com.fanyouzhao.eshop.portal.exception.BusinessException;
import com.fanyouzhao.eshop.portal.service.GoodsService;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: fanyouzhao
 * @date: 16:32 2021/7/2
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品模块")
public class GoodsController {

    @Resource
    GoodsService goodsService;

    @PostMapping("/goodsDetail")
    public JsonResult goodsDetail(Long id) throws BusinessException {
        Goods goods = goodsService.queryGoodsByCacheId(id);
        return new JsonResult("1", "success", goods);
    }

    @PostMapping("/seckill")
    public JsonResult goodsSeckill(Long goodsId, Integer userId) throws BusinessException {
        Boolean seckill = goodsService.seckill(goodsId, userId);
        if (seckill) {
            return new JsonResult("1", "秒杀成功！");
        } else {
            System.out.println("秒杀失败");
            return new JsonResult("0", "秒杀失败！");
        }
    }

    @PostMapping("/refresh")
    public JsonResult goodsRefreshById(Long id){
        return null;
    }
}
